package com.yst.metho.web.application.utils;

import java.text.MessageFormat;

import nextapp.echo2.app.Extent;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.components.MessagePopupWindow;
import com.yst.metho.web.application.components.msgpane.ErrorPane;
import com.yst.metho.web.application.components.msgpane.InformationPane;
import com.yst.suivi.services.exceptions.BusinessException;

public final class ExceptionMgt
{

	private static final String CONFIRMATION_MESSAGE = "L erreur suivante est survenue:\n {0}?";

	public static void manage(Throwable t)
	{
		Object[] params = new Object[1];
		params[0] = t.getMessage();
		if (t instanceof BusinessException)
		{
			BusinessException be = (BusinessException) t;
			MessagePopupWindow window = new MessagePopupWindow(new ErrorPane(be.getLocalizedMessage()), "Erreur");
			window.setHeight(new Extent(200, Extent.PX));
			SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
		}
		else
		{
			MessagePopupWindow window = new MessagePopupWindow(new ErrorPane(MessageFormat.format(CONFIRMATION_MESSAGE, params)), "Erreur");
			window.setHeight(new Extent(200, Extent.PX));
			SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
		}

	}

	public static void displayMessageBox(String msg)
	{
		MessagePopupWindow window = new MessagePopupWindow(new InformationPane(msg), "Information");
		window.setHeight(new Extent(200, Extent.PX));
		SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
	}
}
